/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2013 Magister Solutions Ltd
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Sami Rantanen <sami.rantanen@magister.fi>
 */

#include "satellite-simple-net-device.h"

#include <ns3/log.h>
#include <ns3/node.h>
#include <ns3/pointer.h>
#include <ns3/uinteger.h>

NS_LOG_COMPONENT_DEFINE("SatSimpleNetDevice");

namespace ns3
{

NS_OBJECT_ENSURE_REGISTERED(SatSimpleNetDevice);

TypeId
SatSimpleNetDevice::GetTypeId(void)
{
    static TypeId tid =
        TypeId("ns3::SatSimpleNetDevice")
            .SetParent<NetDevice>()
            .AddConstructor<SatSimpleNetDevice>()
            .AddAttribute(
                "Mtu",
                "The MAC-level Maximum Transmission Unit",
                UintegerValue(0xffff), // 65535 bytes.
                MakeUintegerAccessor(&SatSimpleNetDevice::SetMtu, &SatSimpleNetDevice::GetMtu),
                MakeUintegerChecker<uint16_t>())
            .AddAttribute("ReceiveErrorModel",
                          "The receiver error model used to simulate packet loss",
                          PointerValue(),
                          MakePointerAccessor(&SatSimpleNetDevice::m_receiveErrorModel),
                          MakePointerChecker<ErrorModel>())
            .AddTraceSource(
                "PhyRxDrop",
                "Trace source indicating a packet has been dropped by the device during reception",
                MakeTraceSourceAccessor(&SatSimpleNetDevice::m_phyRxDropTrace),
                "ns3::Packet::TracedCallback");
    return tid;
}

SatSimpleNetDevice::SatSimpleNetDevice()
    : m_channel(0),
      m_node(0),
      m_mtu(0xffff),
      m_ifIndex(0)
{
    NS_LOG_FUNCTION(this);
}

SatSimpleNetDevice::~SatSimpleNetDevice()
{
    NS_LOG_FUNCTION(this);
}

void
SatSimpleNetDevice::Receive(Ptr<Packet> packet,
                            uint16_t protocol,
                            Mac48Address to,
                            Mac48Address from)
{
    NS_LOG_FUNCTION(this << packet << protocol << to << from);
    NetDevice::PacketType packetType;

    if (m_receiveErrorModel && m_receiveErrorModel->IsCorrupt(packet))
    {
        m_phyRxDropTrace(packet);
    }
    else
    {
        if (to == m_address)
        {
            packetType = NetDevice::PACKET_HOST;
        }
        else if (to.IsBroadcast())
        {
            packetType = NetDevice::PACKET_HOST;
        }
        else if (to.IsGroup())
        {
            packetType = NetDevice::PACKET_MULTICAST;
        }
        else
        {
            packetType = NetDevice::PACKET_OTHERHOST;
        }

        if (packetType != NetDevice::PACKET_OTHERHOST)
        {
            m_rxCallback(this, packet, protocol, from);

            if (!m_promiscCallback.IsNull())
            {
                m_promiscCallback(this, packet, protocol, from, to, packetType);
            }
        }
    }
}

void
SatSimpleNetDevice::SetChannel(Ptr<SatSimpleChannel> channel)
{
    NS_LOG_FUNCTION(this << channel);
    m_channel = channel;
    m_channel->Add(this);
}

void
SatSimpleNetDevice::SetReceiveErrorModel(Ptr<ErrorModel> em)
{
    NS_LOG_FUNCTION(this << em);
    m_receiveErrorModel = em;
}

void
SatSimpleNetDevice::SetIfIndex(const uint32_t index)
{
    NS_LOG_FUNCTION(this << index);
    m_ifIndex = index;
}

uint32_t
SatSimpleNetDevice::GetIfIndex(void) const
{
    NS_LOG_FUNCTION(this);
    return m_ifIndex;
}

Ptr<Channel>
SatSimpleNetDevice::GetChannel(void) const
{
    NS_LOG_FUNCTION(this);
    return m_channel;
}

void
SatSimpleNetDevice::SetAddress(Address address)
{
    NS_LOG_FUNCTION(this << address);
    m_address = Mac48Address::ConvertFrom(address);
}

Address
SatSimpleNetDevice::GetAddress(void) const
{
    //
    // Implicit conversion from Mac48Address to Address
    //
    NS_LOG_FUNCTION(this);
    return m_address;
}

bool
SatSimpleNetDevice::SetMtu(const uint16_t mtu)
{
    NS_LOG_FUNCTION(this << mtu);
    m_mtu = mtu;
    return true;
}

uint16_t
SatSimpleNetDevice::GetMtu(void) const
{
    NS_LOG_FUNCTION(this);
    return m_mtu;
}

bool
SatSimpleNetDevice::IsLinkUp(void) const
{
    NS_LOG_FUNCTION(this);
    return true;
}

void
SatSimpleNetDevice::AddLinkChangeCallback(Callback<void> callback)
{
    NS_LOG_FUNCTION(this << &callback);
}

bool
SatSimpleNetDevice::IsBroadcast(void) const
{
    NS_LOG_FUNCTION(this);
    return true;
}

Address
SatSimpleNetDevice::GetBroadcast(void) const
{
    NS_LOG_FUNCTION(this);
    return Mac48Address("ff:ff:ff:ff:ff:ff");
}

bool
SatSimpleNetDevice::IsMulticast(void) const
{
    NS_LOG_FUNCTION(this);
    return true;
}

Address
SatSimpleNetDevice::GetMulticast(Ipv4Address multicastGroup) const
{
    NS_LOG_FUNCTION(this << multicastGroup);
    return Mac48Address::GetMulticast(multicastGroup);
}

Address
SatSimpleNetDevice::GetMulticast(Ipv6Address addr) const
{
    NS_LOG_FUNCTION(this << addr);
    return Mac48Address::GetMulticast(addr);
}

bool
SatSimpleNetDevice::IsPointToPoint(void) const
{
    NS_LOG_FUNCTION(this);
    return false;
}

bool
SatSimpleNetDevice::IsBridge(void) const
{
    NS_LOG_FUNCTION(this);
    return false;
}

bool
SatSimpleNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
{
    NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
    Mac48Address to = Mac48Address::ConvertFrom(dest);
    m_channel->Send(packet, protocolNumber, to, m_address, this);
    return true;
}

bool
SatSimpleNetDevice::SendFrom(Ptr<Packet> packet,
                             const Address& source,
                             const Address& dest,
                             uint16_t protocolNumber)
{
    NS_LOG_FUNCTION(this << packet << source << dest << protocolNumber);
    Mac48Address to = Mac48Address::ConvertFrom(dest);
    Mac48Address from = Mac48Address::ConvertFrom(source);
    m_channel->Send(packet, protocolNumber, to, from, this);
    return true;
}

Ptr<Node>
SatSimpleNetDevice::GetNode(void) const
{
    NS_LOG_FUNCTION(this);
    return m_node;
}

void
SatSimpleNetDevice::SetNode(Ptr<Node> node)
{
    NS_LOG_FUNCTION(this << node);
    m_node = node;
}

bool
SatSimpleNetDevice::NeedsArp(void) const
{
    NS_LOG_FUNCTION(this);
    return true;
}

void
SatSimpleNetDevice::SetReceiveCallback(NetDevice::ReceiveCallback cb)
{
    NS_LOG_FUNCTION(this << &cb);
    m_rxCallback = cb;
}

void
SatSimpleNetDevice::DoDispose(void)
{
    NS_LOG_FUNCTION(this);
    m_channel = 0;
    m_node = 0;
    m_receiveErrorModel = 0;
    NetDevice::DoDispose();
}

void
SatSimpleNetDevice::SetPromiscReceiveCallback(PromiscReceiveCallback cb)
{
    NS_LOG_FUNCTION(this << &cb);
    m_promiscCallback = cb;
}

bool
SatSimpleNetDevice::SupportsSendFrom(void) const
{
    NS_LOG_FUNCTION(this);
    return true;
}

} // namespace ns3
